package cc.rengu.igas.cops.core.service.trans.payment;

import cc.rengu.igas.cops.common.constant.CopsParamConstant;
import cc.rengu.igas.cops.common.dao.impl.CopsSysParamMapperImpl;
import cc.rengu.igas.cops.common.enums.CopsRespCodeEnum;
import cc.rengu.igas.cops.core.service.base.ManageService;
import cc.rengu.igas.cops.facade.request.BankInfoQueryByCardNoRequest;
import cc.rengu.igas.cops.facade.response.BankInfoQueryByCardNoResponse;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.BinInfoService;
import cc.rengu.oltp.service.realize.impl.BinInfoServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;

import java.util.List;

/**
 * 01560000803620	卡号对应行号及银行信息查询
 *
 * @author lzl
 * @version 1.0
 * @date 2020/07/9
 */

public class BankInfoQueryByCardNoService extends ManageService {


    @Override
    protected Object bizBeforeCust() throws Exception {
        BankInfoQueryByCardNoRequest request = new BankInfoQueryByCardNoRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BankInfoQueryByCardNoRequest queryRequest = (BankInfoQueryByCardNoRequest) request;
        BankInfoQueryByCardNoResponse queryResponse = new BankInfoQueryByCardNoResponse();

        BinInfoService binInfoService = new BinInfoServiceImpl();
        BinInfo binInfo = binInfoService.getCardBinInfo(queryRequest.getAcctNo());
        if (null == binInfo) {
            rglog.error("通过卡号查询卡bin信息为空！处理失败 acctNo:<{}>", queryRequest.getAcctNo());
            throw new BizException(CopsRespCodeEnum.SIGN_FAIL05.getRespCode(), CopsRespCodeEnum.SIGN_FAIL05.getRespDesc());
        }

        //查询该法人行的银联机构号
        CopsSysParamMapperImpl sysParamService = new CopsSysParamMapperImpl();
        List<SysParam> sysParamList = sysParamService.getSysParamInfoList(CopsParamConstant.MCHNT_PARAM_TYPE, CopsParamConstant.CUPS_INSR_ID);
        if (null == sysParamList) {
            rglog.error("通过paramType:<{}>,paramKey:<{}>查询信息为空！处理失败 ", CopsParamConstant.MCHNT_PARAM_TYPE, CopsParamConstant.CUPS_INSR_ID);
            throw new BizException(CopsRespCodeEnum.PAYTRANSFER_FAIL01.getRespCode(), CopsRespCodeEnum.PAYTRANSFER_FAIL01.getRespDesc());
        }

        /*判断是否匹配*/
        String instId = "";
        for (SysParam sysParam : sysParamList) {
            if (binInfo.getIssuerCode().substring(0, 4).equals(sysParam.getParamValue().substring(0, 4))) {
                instId = sysParam.getInstId();
                break;
            }
        }

        /*卡BIN类型转换  银联枚举1-借记卡2-贷记卡3-准贷记卡4-预付卡
         * 报文枚举 00贷记 01借记  02准贷记  03预付费卡*/
        if ("1".equals(binInfo.getCardType())) {
            queryResponse.setAcctType("01");
        } else if ("2".equals(binInfo.getCardType())) {
            queryResponse.setAcctType("00");
        } else if ("3".equals(binInfo.getCardType())) {
            queryResponse.setAcctType("02");
        } else if ("4".equals(binInfo.getCardType())) {
            queryResponse.setAcctType("03");
        }
        queryResponse.setInstId(instId);
        queryResponse.setIssUerId(binInfo.getIssuerCode());
        queryResponse.setIssUerName(binInfo.getIssuerName());
        queryResponse.setCardName(binInfo.getCardName());

        BizResponse<BankInfoQueryByCardNoResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(queryResponse);
        bizResponse.setRspSysId(CopsParamConstant.IGAS);
        bizResponse.setRespCode(CopsRespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(CopsRespCodeEnum.TRANS_SUCCESS.getRespDesc());

        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        BankInfoQueryByCardNoResponse responseMsg = (BankInfoQueryByCardNoResponse) bizResponse.getResult();
        ConvertUtil.convertInput(responseMsg);
    }
}
